﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Blader.ActionHandler;
using Blader.Cache;
using Blader.Config;
using Blader.UI.TextBox;
using Blader.UI.Win32;

namespace Blader.UI.Forms
{
    public partial class SearchForm : Form
    {
        private readonly HandlerContext handlerContext;

        public SearchForm(HandlerContext handlerContext)
        {
            this.handlerContext = handlerContext;

            InitializeComponent();

            CacheService cacheService = CacheService.GetCacheService();

            if (handlerContext.IsInVs)
            {
                if (!string.IsNullOrEmpty(handlerContext.ApplicationObject.Solution.FileName))
                {
                    AutoCompleteEntryCollection files = cacheService.LoadSolutionSqlFiles(handlerContext.ApplicationObject.Solution.FileName);
                    SearchTextBox.Items = files;
                }
            }
            else
            {
                AutoCompleteEntryCollection files = cacheService.LoadProjectSqlFiles();
                SearchTextBox.Items = files;
            }

            InitLocation();
        }

        private void InitLocation()
        {
            int x;
            int.TryParse(IniFile.Instance.IniReadValue("SearchForm", "Pos-X"), out x);
            int y;
            int.TryParse(IniFile.Instance.IniReadValue("SearchForm", "Pos-Y"), out y);
            Location = new Point(x, y);
        }

        private void SearchTextBox_EntrySelectedEvent(object sender, IAutoCompleteEntry entry)
        {
            Native.ShellExecute(IntPtr.Zero, "open", entry.Value, null, null, 3);
            Close();
        }

        private void SearchForm_Deactivate(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void SearchForm_Activated(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        public bool HasFocusOnChile(Control.ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                if (control.Focused) return true;
                if (HasFocusOnChile(control.Controls)) return true;
            }
            return false;
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == WMMessages.WM_NCHITTEST && m.Result.ToInt32() == WMMessages.HTCLIENT)
            {
                m.Result = new IntPtr(WMMessages.HTCAPTION);
            }
        }

        private void SearchForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            IniFile.Instance.IniWriteValue("SearchForm", "Pos-X", Location.X.ToString());
            IniFile.Instance.IniWriteValue("SearchForm", "Pos-Y", Location.Y.ToString());
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ActiveForm == null)
            {
                Close();
                timer1.Stop();
            }
        }
    }
}
